#!/usr/bin/env bash
set -euo pipefail

# 需要先安装并配置 coscmd：
# pip3 install coscmd
# coscmd config -a <SECRET_ID> -s <SECRET_KEY> -b <BUCKET-NAME> -r <REGION>

SITE_DIR="${SITE_DIR:-website}"
COS_BUCKET="${COS_BUCKET:-your-bucket-12345}"
COS_REGION="${COS_REGION:-ap-beijing}"
COS_PROTOCOL="${COS_PROTOCOL:-http}"

if [[ ! -d "$SITE_DIR" ]]; then
  echo "[ERROR] 站点目录不存在: $SITE_DIR"
  exit 1
fi

# 配置（如果未配置全局）
coscmd config -b "$COS_BUCKET" -r "$COS_REGION" || true

# 递归上传到桶根路径
coscmd upload -r "$SITE_DIR/" "/" --delete

echo "[INFO] 上传完成: cos://${COS_BUCKET}/"
echo "[NEXT] 请在控制台开启静态网站托管，并设置首页和错误页为 index.html。"
echo "[NEXT] 访问链接示例: ${COS_PROTOCOL}://${COS_BUCKET}.cos.${COS_REGION}.myqcloud.com"